<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Hd_invitation_validate extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Hdinvitationcustomermodel','m_customer');
    }

    public function index(){
        $data = parent::index();

        global $g_hd_invitation_gift_type;
        $data['game_gift'] = array4js($g_hd_invitation_gift_type);

        $this->load->view('admin/hd_invitation_validate',$data);
    }

    public function getCode() {
        $this->validate->setRule('code','string');
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $lottery_history = $this->m_customer->getLotteryHistoryByCode($info['code']);
        if(empty($lottery_history)) {
            $this->response->setDetail('卡券号错误!');
            $this->response->responseExit();
        }
        if($lottery_history['gift_status'] == STATUS_ACTIVE) {
            $this->response->setDetail('卡券号已被领取激活!');
            $this->response->responseExit();
        }

        $this->response->setSuccess();
        $this->response->setAddon($lottery_history);
        $this->response->responseExit();
    }

    public function validateCode() {
        $this->validate->setRule('code','string');
        $this->validate->setRule('customer_name','string');
        $this->validate->setRule('customer_car_number','string');
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $lottery_history = $this->m_customer->getLotteryHistoryByCode($info['code']);
        if(empty($lottery_history)) {
            $this->response->setDetail('卡券号错误!');
            $this->response->responseExit();
        }
        if($lottery_history['gift_status'] == STATUS_ACTIVE) {
            $this->response->setDetail('卡券号已被领取激活!');
            $this->response->responseExit();
        }
        $customer = $this->m_customer->getCustomer($lottery_history['customer_id']);

        $info['lottery_history_id'] = $lottery_history['lottery_history_id'];
        $info['lottery_time_time_active'] = NOW;
        $info['gift_status'] = STATUS_ACTIVE;
        $info['user_id'] = $this->m_app->getCurrentUserId();
        $info['validate_source'] = $customer['create_source'];
        $this->m_customer->editLotteryHistory($info);
        $this->m_customer->editCustomer(array(
            'customer_id' => $lottery_history['customer_id'],
            'customer_name' => $info['customer_name'],
            'customer_car_number' => $info['customer_car_number'],
            'gift_status' => STATUS_ACTIVE,
            'validate_source' => $customer['create_source'],
        ));

        $this->response->setSuccess();
        $this->response->responseExit();
    }

}
/* End of file aa_game_history.php */
/* Location: ./application/controllers/aa_game_history.php */
